<?php

class Application_Model_DbTable_ApiLog extends Zend_Db_Table_Abstract
{

    protected $_name = 'apilog';	
	protected $select;
	
	public function getDates($from, $to) {
		$slct = $this->select()->from($this, array('ldatetime'))->distinct()->order('ldatetime');
		
		if ($from != null) {
			$slct->where("To_DAYS(ldatetime) >= To_DAYS(?)", $from);
		}
		if ($to != null) {
			$slct->where("To_DAYS(ldatetime) <= To_DAYS(?)", $to);
		}	
		
		return $this->fetchAll($slct);		
	}
	
	
	public function  getData($page, $sort, $mincount, $from, $to, &$itemcount) {
		$this->select->having('COUNT(lapi_name) >= ?', $mincount);
		
		if ($from != null) {
			$this->select->where("To_DAYS(ldatetime) >= To_DAYS(?)", $from);
		}
		if ($to != null) {
			$this->select->where("To_DAYS(ldatetime) <= To_DAYS(?)", $to);
		}
		
		
		
		
		$itemcount = $this->fetchAll($this->select)->count() ;
		$this->select->limitPage($page, MAXITEM);
		
		
		
		if ($sort != '') {
			if ($sort == 'name') {
				$this->select->order('lapi_name');
			} else {
				$this->select->order('count desc');
			}
		}
		
		
		
		
		$rows = $this->fetchAll($this->select);
		return $rows;		
	}
	
	public function cau1($page, $sort, $mincount, $from, $to, &$itemcount) {		
		$this->select = $this->select()->from($this, array('count' => 'COUNT(lapi_name)', 'lapi_name'))->group('lapi_name');
		return $this->getData($page, $sort, $mincount, $from, $to, $itemcount);		
	}
	
	public function cau2($page, $sort, $mincount, $from, $to, &$itemcount) {		
    	$this->select = $this->select()->from($this, array('count' => 'COUNT(lapi_name)', 'lapi_name', 'ldatetime'))
    	->group(array('ldatetime', 'lapi_name'));
		return $this->getData($page, $sort, $mincount, $from, $to, $itemcount);
	}

}

